package com.atguigu.classroom.order.service.impl;

import com.atguigu.classroom.exception.ClassroomException;
import com.atguigu.classroom.order.service.WXPayService;
import com.atguigu.classroom.utils.HttpClientUtils;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class WXPayServiceImpl implements WXPayService {
    @Value("${wechat.mpAppId}")
    private String appid;
    @Override
    public Map<String, String> createJsapi(String orderNo) {
        if(true){
            return null;
        }
        Map<String,String> paramMap=new HashMap<>();
        paramMap.put("appid", appid);//正式服务号id
        paramMap.put("mch_id", "1481962542");//正式服务号商户号
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
        //随机数，每次支付都会生成随机数，每次都不一样；
        paramMap.put("body", "test");//支付时微信弹框显示的内容；
        paramMap.put("out_trade_no", orderNo);//订单号
        paramMap.put("total_fee", "1");//支付金额
        paramMap.put("spbill_create_ip", "127.0.0.1");
        //当前支付客户端的ip
        paramMap.put("notify_url", "http://glkt.atguigu.cn/api/order/wxPay/notify");
        //支付后的跳转
        paramMap.put("trade_type", "JSAPI");
        //支付类型，这里是按照生成的固定的金额支付
        paramMap.put("openid", "oQTXC56lAy3xMOCkKCImHtHoLL");
        //支付用户的openid
        /***************HTTPClient来根据URL访问第三方接口并且传递参数****************/
        HttpClientUtils client = new HttpClientUtils("https://api.mch.weixin.qq.com/pay/unifiedorder");
        //client设置参数
        try {
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, "MXb72b9RfshXZD4FRGV5KLqmv5bx9LT9"));
            client.setHttps(true);
            client.post();
            /*******************返回第三方的数据*******************/
            String xml = client.getContent();
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            if(null != resultMap.get("result_code")  && !"SUCCESS".equals(resultMap.get("result_code"))) {
                throw new ClassroomException(20001,"支付失败");
            }

            Map<String, String> parameterMap = new HashMap<>();
            String prepayId = String.valueOf(resultMap.get("prepay_id"));
            String packages = "prepay_id=" + prepayId;
            parameterMap.put("appId", "wxf913bfa3a2c7eeeb");
            parameterMap.put("nonceStr", resultMap.get("nonce_str"));
            parameterMap.put("package", packages);
            parameterMap.put("signType", "MD5");
            parameterMap.put("timeStamp", String.valueOf(new Date().getTime()));
            String sign = WXPayUtil.generateSignature(parameterMap, "MXb72b9RfshXZD4FRGV5KLqmv5bx9LT9");
            /*******************返回结果*******************/
            Map<String, String> result = new HashMap();
            result.put("appId", "wxf913bfa3a2c7eeeb");
            result.put("timeStamp", parameterMap.get("timeStamp"));
            result.put("nonceStr", parameterMap.get("nonceStr"));
            result.put("signType", "MD5");
            result.put("paySign", sign);
            result.put("package", packages);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ClassroomException(20001,"支付失败");
        }
    }
}
